package com.shrct.businessprocess.purchase.impl;

import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.budget.impl.RCHasCostBudgetImpl;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchasePayRequest;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCPayRequest;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCHasPurchaseBudgetProxy;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@Entity
@DiscriminatorValue("RCHASPURCHASEBUDGET_TYPE")
public abstract class RCHasPurchaseBudgetImpl extends RCHasCostBudgetImpl implements RCHasPurchaseBudget {
  public RCHasPurchaseBudgetImpl() {
    super();
  }
  
  public RCHasPurchaseBudgetImpl(final RCCostBreakDown cbd) {
    super( cbd);
    
  }
  
  @OneToMany(targetEntity = RCBasePurchaseRequestImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "budget")
  private Collection<RCBasePurchaseRequest> purchaseRequests = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCBasePurchaseRequest>();;
  
  public Collection<RCBasePurchaseRequest> getPurchaseRequests() {
    return this.purchaseRequests;
  }
  
  public RCHasPurchaseBudget removeAllPurchaseRequests() {
    for(RCBasePurchaseRequest obj : this.purchaseRequests){
    	getObjectFactory().delete(obj);
    }
    this.purchaseRequests.clear();
    return this;	
    
  }
  
  public RCHasPurchaseBudget removeFromPurchaseRequests(final RCBasePurchaseRequest rCBasePurchaseRequest) {
    this.purchaseRequests.remove(rCBasePurchaseRequest);
    getObjectFactory().delete(rCBasePurchaseRequest);
    return this;
    
  }
  
  /**
   * 计算订单总金额
   */
  public RCProjectMoney getTotalOrderAmount() {
    RCProjectMoney _xblockexpression = null;
    {
      final Function1<RCBasePurchaseRequest,Boolean> _function = new Function1<RCBasePurchaseRequest,Boolean>() {
          public Boolean apply(final RCBasePurchaseRequest it) {
            Boolean _isPassed = it.getIsPassed();
            return _isPassed;
          }
        };
      Iterable<RCBasePurchaseRequest> _filter = IterableExtensions.<RCBasePurchaseRequest>filter(this.purchaseRequests, _function);
      final Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>> _function_1 = new Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>>() {
          public Collection<? extends RCBasePurchaseOrder> apply(final RCBasePurchaseRequest it) {
            Collection<? extends RCBasePurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<? extends RCBasePurchaseOrder>> _map = IterableExtensions.<RCBasePurchaseRequest, Collection<? extends RCBasePurchaseOrder>>map(_filter, _function_1);
      Iterable<? extends RCBasePurchaseOrder> _flatten = Iterables.concat(_map);
      final Function1<RCBasePurchaseOrder,Boolean> _function_2 = new Function1<RCBasePurchaseOrder,Boolean>() {
          public Boolean apply(final RCBasePurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<? extends RCBasePurchaseOrder> orders = IterableExtensions.filter(_flatten, _function_2);
      final Function1<RCBasePurchaseOrder,RCProjectMoney> _function_3 = new Function1<RCBasePurchaseOrder,RCProjectMoney>() {
          public RCProjectMoney apply(final RCBasePurchaseOrder it) {
            RCProjectMoney _totalCost = it.getTotalCost();
            return _totalCost;
          }
        };
      Iterable<RCProjectMoney> _map_1 = IterableExtensions.map(orders, _function_3);
      RCExchangeRate _defaultPurchaseExchangeRate = this.getDefaultPurchaseExchangeRate();
      RCProjectMoney _sum = Moneys.sum(_map_1, _defaultPurchaseExchangeRate);
      _xblockexpression = (_sum);
    }
    return _xblockexpression;
  }
  
  public Collection<? extends RCPayRequest> getAllPayRequests() {
    List<RCPayRequest> _xblockexpression = null;
    {
      final Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>> _function = new Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>>() {
          public Collection<? extends RCBasePurchaseOrder> apply(final RCBasePurchaseRequest it) {
            Collection<? extends RCBasePurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<? extends RCBasePurchaseOrder>> _map = IterableExtensions.<RCBasePurchaseRequest, Collection<? extends RCBasePurchaseOrder>>map(this.purchaseRequests, _function);
      Iterable<? extends RCBasePurchaseOrder> _flatten = Iterables.concat(_map);
      final Function1<RCBasePurchaseOrder,Boolean> _function_1 = new Function1<RCBasePurchaseOrder,Boolean>() {
          public Boolean apply(final RCBasePurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      final Iterable<? extends RCBasePurchaseOrder> allOrders = IterableExtensions.filter(_flatten, _function_1);
      final Function1<RCBasePurchaseOrder,Collection<RCBasePurchasePayRequest>> _function_2 = new Function1<RCBasePurchaseOrder,Collection<RCBasePurchasePayRequest>>() {
          public Collection<RCBasePurchasePayRequest> apply(final RCBasePurchaseOrder it) {
            Collection<RCBasePurchasePayRequest> _payRequests = it.getPayRequests();
            return _payRequests;
          }
        };
      Iterable<Collection<RCBasePurchasePayRequest>> _map_1 = IterableExtensions.map(allOrders, _function_2);
      Iterable<RCBasePurchasePayRequest> _flatten_1 = Iterables.<RCBasePurchasePayRequest>concat(_map_1);
      Collection<? extends RCPayRequest> _allPayRequests = super.getAllPayRequests();
      Iterable<RCPayRequest> _plus = Iterables.<RCPayRequest>concat(_flatten_1, _allPayRequests);
      List<RCPayRequest> _list = IterableExtensions.<RCPayRequest>toList(_plus);
      _xblockexpression = (_list);
    }
    return _xblockexpression;
  }
  
  public RCHasPurchaseBudgetProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCHasPurchaseBudgetProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCHasPurchaseBudgetProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
